package com.zjmzxfzhl.modules.app.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import com.zjmzxfzhl.common.core.util.SecurityUtils;
import com.zjmzxfzhl.modules.app.entity.AppDoc;
import com.zjmzxfzhl.modules.app.entity.AppFavor;
import com.zjmzxfzhl.modules.app.form.AppDocVo;
import com.zjmzxfzhl.modules.app.service.AppDocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjmzxfzhl.common.core.Result;
import com.zjmzxfzhl.common.core.base.BaseController;
import com.zjmzxfzhl.modules.app.entity.AppFavor;
import com.zjmzxfzhl.modules.app.service.AppFavorService;

/**
 * Controller
 * 
 * @author 张森荣
 */
@RestController
@RequestMapping("/app/favor")
public class AppFavorController extends BaseController {
    @Autowired
    private AppFavorService appFavorService;

    @Autowired
    private AppDocService appDocService;
    /**
     * 自定义查询列表
     * 
     * @param appFavor
     * @param current
     * @param size
     * @return
     */
//    @PreAuthorize("@elp.single('app:favor:list')")
    @GetMapping(value = "/list")
    public Result list(AppFavor appFavor, @RequestParam Integer current, @RequestParam Integer size) {
        Map<String,Object> resultMap=new HashMap();
        AppDocVo appDocVo=new AppDocVo();
        String userId = SecurityUtils.getUserId();
        if (userId == null) {
            return Result.error("找不到用户");
        }
        appDocVo.setUserId(userId);
        appDocVo.setDeleted("0");

        IPage<AppDocVo> pageList = appDocService.list(new Page<AppDocVo>(current, size), appDocVo);
        resultMap.put("data",pageList);

        Integer total=appDocService.selectRowCount(appDocVo);
        resultMap.put("total",total);
        return Result.ok(resultMap);
    }

    @PreAuthorize("@elp.single('app:favor:list')")
    @GetMapping(value = "/queryById")
    public Result queryById(@RequestParam String id) {
        AppFavor appFavor = appFavorService.getById(id);
        return Result.ok(appFavor);
    }

    /**
     * @功能：新增
     * @param appFavor
     * @return
     */
//    @PreAuthorize("@elp.single('app:favor:save')")
    @PostMapping(value = "/save")
    public Result save(@Valid @RequestBody AppFavor appFavor) {
        String userId = SecurityUtils.getUserId();
        if (userId == null) {
            return Result.error("找不到用户");
        }
        appFavor.setUserId(userId);
        Boolean result=appFavorService.saveAppFavor(appFavor);
        if(result==false){
            return Result.error("文档不能重复收藏！");
        }
        return Result.ok();
    }

    /**
     * @功能：修改
     * @param appFavor
     * @return
     */
    @PreAuthorize("@elp.single('app:favor:update')")
    @PutMapping(value = "/update")
    public Result update(@Valid @RequestBody AppFavor appFavor) {
        appFavorService.updateById(appFavor);
        return Result.ok();
    }

    /**
     * @功能：批量删除
     * @param ids
     * @return
     */
    @PreAuthorize("@elp.single('app:favor:delete')")
    @DeleteMapping(value = "/delete")
    public Result delete(@RequestParam String ids) {
        if (ids == null || ids.trim().length() == 0) {
            return Result.error("ids can't be empty");
        }
        String[] idsArr = ids.split(",");
        if (idsArr.length > 1) {
            appFavorService.removeByIds(Arrays.asList(idsArr));
        } else {
            appFavorService.removeById(idsArr[0]);
        }
        return Result.ok();
    }

    /**
     * @功能：取消收藏
     * @param docId
     * @return
     */
//    @PreAuthorize("@elp.single('app:projGit:delete')")
    @DeleteMapping(value = "/relationDel")
    public Result relationDel(@RequestParam String docId) {
        AppFavor appFavor=new AppFavor();
        appFavor.setDocId(docId);
        String userId = SecurityUtils.getUserId();
        if (userId == null) {
            return Result.error("找不到用户");
        }
        appFavor.setUserId(userId);
        Boolean result=appFavorService.relationDel(appFavor);
        if(result==false){
            return Result.error("删除关联失败！");
        }
        return Result.ok();
    }
}
